package com.larry.rabbitmq.first;

import com.rabbitmq.client.*;

/**
 * @author larry
 * @Title: larry-rabbitmq
 * @Package com.larry.rabbitmq.first
 * @Description: 消费者  --  接收消息
 * @date 6/28/214:29 PM
 */
public class Consumer {
    public static final String QUEUE_NAME = "hello";

    public static void main(String args[]) throws Exception{
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("47.93.55.0");
        factory.setUsername("guest");
        factory.setPassword("guest");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();



        //声明 接收消息
        DeliverCallback deliverCallback = (consumerTag,message) -> {
            System.out.println(new String(message.getBody()));
        };
        //取消消息时的回调
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("消息消费被中断.......");
        };


        /**
         * 消费消息
         *  消费的队列
         *  消费成功后是否要自动应答
         *  消费者未成功消费的回调函数
         *  消费者取消消费的回调
         */
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);
    }
}
